/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.sales;

import dao.BaseDaoHibernate;
import model.sales.SalespendingMst;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

/**
 *
 * @author jay
 */
@Repository
public class SalespendingDao extends BaseDaoHibernate<SalespendingMst>{

    private static final Logger log = Logger.getLogger(SalespendingDao.class);
    
    public boolean saveData(SalespendingMst domain) {
        Session session = sessionFactory.openSession();
        boolean saved = false;
        try {
            session.beginTransaction();
                                
            domain.setSalespendingDetails(domain.getSalespendingDetails());
            sessionFactory.getCurrentSession().saveOrUpdate(domain);
            
            long orderId = domain.getId();
            for (int i=0; i<domain.getSalespendingDetails().size(); i++) {
                domain.getSalespendingDetails().get(i).getId().setId(orderId);
                domain.getSalespendingDetails().get(i).setSeq(i);
            }

            session.getTransaction().commit();
            saved=true;
        } catch (RuntimeException e) {
            session.getTransaction().rollback(); 
            log.error("gagal", e);
            java.util.logging.Logger.getLogger(SalespendingDao.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
        } finally {
            if (session != null) { session.close();}
            return saved;
        }
    }
    
}
